<?php

$config = Config::getInstance();

$dt_ini = $config->filter('dt-inicial');
$dt_fim = $config->filter('dt-final');

include_once CONTROLLERS.'loja.php';
$loja_controller = new LojaController();
$loja   = $config->filter('loja');
if(empty($loja)){
    $lojas = $config->currentController->getAllLojas();
} else {
    foreach($loja as $lid){ $lojas[] = $loja_controller->getLoja($lid); }
}


echo '  <p class=\'name\'> Resumo de vendas das equipes de venda. Gerado em <b>'.date('d/m/Y').'</b> de 
        <b>'.$config->maskData($dt_ini).'</b> à <b>'.$config->maskData($dt_fim).'</b></p> ';


include_once CONTROLLERS.'venda.php';
$venda_controller = new VendaController();

echo '<div class=\'content\'>';

$t_ini = strtotime($dt_ini);
$t_fim = strtotime($dt_fim);

$periodos = array();

$current    = $t_ini;       
while($current <= $t_fim){
    $periodos[]     = (object) array("name" =>date('d/m/Y', $current), "date" => date("Y-m-d", $current));
    $current        = strtotime("+1 day", $current);  
}

foreach($lojas as $loja){
    
    echo '<p class=\'name\'>'.$loja->sigla.'</p>';
    
    $equipes = $config->currentController->getEquipesByLoja($loja->id, true);
    
    echo '<div class=\'content\'>';
    
    echo '<table>';
    echo '<thead>';
    echo '<tr>';
    echo '<th>Equipe</th>';
    echo '<th>Líder</th>';
    foreach($periodos as $p){ echo '<th>' . $p->name . '</th>'; }
    echo '<th>Total</th>';
    echo '<th>Equipe</th>';
    echo '</tr>';
    echo '</thead>';
    echo '<tbody>';
    foreach ($equipes as $equipe){
        
        echo '<tr>';
        echo '<td>'.$equipe->nome.'</td>';
        echo '<td>'.$equipe->lider.'</td>';
        
        $agentes    = $config->currentController->getIntegrantes($equipe);
        $ids        = array(); 
        foreach($agentes as $a){ $ids[] = $a->funcionario; }
        
        $total_val = $total_qtd = 0;
        foreach($periodos as $p){ 
            $vendas = $venda_controller->getVendasByIntegranteDeEquipe($ids, $p->date, $p->date);
            $val = 0;
            $qtd = count($vendas);
            foreach($vendas as $v){ $val += $venda_controller->getValorOfVenda($v->id); }
            echo '<td>R$ '.$config->maskDinheiro($val).' ('.$qtd.')</td>';
            $total_val += $val;
            $total_qtd += $qtd;
        }
        echo '<td>R$ '.$config->maskDinheiro($total_val).' ('.$total_qtd.')</td>';
        echo '<td>'.$equipe->nome.'</td>';
        echo '</tr>';
    }
    echo '</tbody>';
    echo '</table>';
    echo '</div>';
    
}

echo '</div>';

?>
<style>
.content table {text-align: center; width: 100%;}
.content table thead th { background: gray; color: white; padding: 5px; border: #666 solid 1px;}
.content tbody tr td { padding: 5px; border-bottom: lightgray solid 1px; border-left: lightgray solid 1px; }
.content tbody tr:nth-child(even) { background: #eee; }
.content {display: inline-block;}
</style>
